package org.balthie.demo.jdk.io;



import java.io.*;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

/**
 * @author：balthie@126.com
 * @createtime ： 2023年1月6日 上午10:31:42
 * @description ————————————————
版权声明：本文为CSDN博主「怪叔叔萝莉控」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
原文链接：https://blog.csdn.net/eava_wu/article/details/119791922
 * @since version 初始于版本 TODO
 */
public class RandomAccessFileTest2 {
    public void write(String path) throws IOException {
        File file = new File(path);
        System.out.println(file.exists());
        FileOutputStream fileOutputStream = new FileOutputStream(file,false);
        fileOutputStream.write("hahaha".getBytes());
        fileOutputStream.flush();
        fileOutputStream.close();
    }


    public void writeNIO(String path) throws IOException {
        File file = new File(path);
        RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
        FileChannel channel = randomAccessFile.getChannel();
        //重置channel到0，如果是第一次启动，不用重置
        channel.position(0);
        ByteBuffer buffer = ByteBuffer.allocate("xhh is yyds".getBytes("UTF-8").length);
        CharBuffer decode = StandardCharsets.UTF_8.decode(buffer);
        //position会到最后
        decode.append(new String("xhh is yyds".getBytes("UTF-8")));
        //交换 position limit
        decode.flip();
        ByteBuffer encode = StandardCharsets.UTF_8.encode(decode);
        channel.write(encode);
        channel.force(true);
        channel.close();
        buffer.clear();
    }

    public String read(String path) throws IOException {
        File file = new File(path);
        RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
        byte[] bytes = new byte[(int) file.length()];
        randomAccessFile.readFully(bytes);
        return new String(bytes);
    }

    public String readNIO(String path) throws IOException {
        File file = new File(path);
        RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
        FileChannel channel = randomAccessFile.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate((int) file.length());
        channel.read(buffer);
        //position走到最后面，需要swap position limit
        buffer.flip();
        //使用UTF-8解码,
        CharBuffer decode = StandardCharsets.UTF_8.decode(buffer);
        channel.close();
        buffer.clear();
        return decode.toString();
    }

    public String readNIODecodeMistake(String path) throws IOException {
        File file = new File(path);
        RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
        FileChannel channel = randomAccessFile.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate((int) file.length());
        channel.read(buffer);
        buffer.flip();
        //使用UTF-8解码
        CharBuffer decode = buffer.asCharBuffer();
        channel.close();
        buffer.clear();
        return decode.toString();
    }

    public static void main(String args[])  {
        RandomAccessFileTest2 randomAccessFileTest = new RandomAccessFileTest2();
        try {
            randomAccessFileTest.write("haha.txt");
            String read = randomAccessFileTest.read("haha.txt");
            System.out.println("read =>"+read);
            randomAccessFileTest.writeNIO("haha.txt");
            System.out.println("readNIO=>"+randomAccessFileTest.readNIO("haha.txt"));
            System.out.println("readNIO readNIODecodeMistake=>"+randomAccessFileTest.readNIODecodeMistake("haha.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

